# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: leon

from concurrent.futures import ProcessPoolExecutor

from absl import app, flags

from coin.base.datetime_util import (convert_string_to_date, iterate_date)
from coin.database.builder.cache_builder import build_cache_batch

FLAGS = flags.FLAGS


def main(argv):
  start_date = FLAGS.start_date
  assert start_date, '--start_date must be specified.'
  end_date = FLAGS.end_date
  assert end_date, '--end_date must be specified.'

  start_date = convert_string_to_date(start_date)
  end_date = convert_string_to_date(end_date)
  trading_dates = iterate_date(start_date, end_date)

  with ProcessPoolExecutor(max_workers=16) as executor:
    for trading_date in trading_dates:
      executor.submit(build_cache_batch, trading_date.strftime('%Y%m%d'))

  return 0


if __name__ == '__main__':
  flags.DEFINE_string('start_date', None, 'yyyymmdd')

  flags.DEFINE_string('end_date', None, 'yyyymmdd. Feed at end_date is not checked.')

  app.run(main)
